﻿#include "View/MainWindow.hpp"
#include "ViewModel/MainWindowViewModel.hpp"

#include <osg\State>

#include <QTranslator>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QFile>

#include <cstdio>
#include <cstdlib>
#include <iostream>

#include <Windows.h>

// 自定义消息处理程序
void Tmp_OnQtLogMessages(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    switch (type)
    {
    case QtDebugMsg:
        fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtInfoMsg:
        fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtWarningMsg:
        fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtCriticalMsg:
        fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        break;
    case QtFatalMsg:
        fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
        //abort();
        break;
    }

#ifdef _DEBUG

    ::OutputDebugString(msg.toStdWString().c_str());
    ::OutputDebugString(L"\n");

#endif
}

int main(int argc, char *argv[])
{
    //HWND hWnd = ::GetConsoleWindow();
    //if (hWnd != nullptr)
    //{
    //    ::ShowWindow(hWnd, SW_HIDE);
    //}

    // freopen("log.txt", "w", stderr);
    // freopen("log.txt", "w", stdout);

    osg::setNotifyLevel(osg::NotifySeverity::ALWAYS);

    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
    QApplication a(argc, argv);

    // 安装消息处理程序
    qInstallMessageHandler(Tmp_OnQtLogMessages);

    QString tmpCIMDataSetJsonFilePath = "D:\\chenxi_Tests\\Requirements\\IfcToAcim\\SceneModel\\BasicInfo.CIM";
    long tmpLevel = 4;

    // 第一个参数是 CIMDataSet 的 JsonFile 的 Path, 第二个参数是 Level
    if (argc < 3)
    {
        //return 0;
    }
    else if (argc >= 3)
    {
        tmpCIMDataSetJsonFilePath = QString::fromStdString(argv[1]);

        char* endPtr = nullptr;
        /*auto level*/ tmpLevel = std::strtol(argv[2], &endPtr, 10);
        if (endPtr == argv[2])
        {
            std::cerr << "Invalid number." << std::endl;
            //return 1;

            tmpLevel = 4;
        }
    }

    auto filePath = tmpCIMDataSetJsonFilePath; // QString::fromStdString(argv[1]);
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return 0;
    }

    QFileInfo CIMPath(filePath);

    MainWindowViewModel::getInstance()->iCIMDataSet = CreateCIMDataSet();
    MainWindowViewModel::getInstance()->iCIMDataSet->fromJson(filePath.toStdString().c_str());

    for (auto i = 0; i < MainWindowViewModel::getInstance()->iCIMDataSet->getSyntheticCount(); i++)
    {
        MainWindowViewModel::getInstance()->CIMList.append(QString::fromStdString(MainWindowViewModel::getInstance()->iCIMDataSet->getSyntheticName(i)));
        
        MainWindowViewModel::getInstance()->filePathList.append(CIMPath.absolutePath() +
                                    QString::fromStdString(MainWindowViewModel::getInstance()->iCIMDataSet->getSyntheticPath(i)).mid(1));
    }

    MainWindowViewModel::getInstance()->standardFilePath = CIMPath.absolutePath() + QString::fromStdString(MainWindowViewModel::getInstance()->iCIMDataSet->getSchemaPath()).mid(1);

    MainWindow mainWindow;
    mainWindow.setMinimumSize(1600, 800);
    mainWindow.setWindowIcon(QIcon("./image/Earth.png"));
    
    mainWindow.setWindowTitle(
            QString::fromStdString(MainWindowViewModel::getInstance()->iCIMDataSet->getName()) + "-" +
            QString::fromStdString(MainWindowViewModel::getInstance()->iCIMDataSet->getVersion()));

    mainWindow.showMaximized();

    //char *endPtr = nullptr;
    auto level = tmpLevel; // std::strtol(argv[2], &endPtr, 10);
    //if (endPtr == argv[2])
    //{
    //    std::cerr << "Invalid number." << std::endl;
    //    return 1;
    //}

    if(level > 0 && level <= 7)
    {
        MainWindow::initEntityFile(MainWindowViewModel::getInstance()->filePathList[level - 1]);
    }

    return QApplication::exec();
}